/**
 * 
 */
package ar.rhok.yerbus.core.dao.impl;

import java.io.Serializable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import ar.rhok.yerbus.core.dao.GenericDAO;

/**
 * @author Mauro Monti
 *
 */
public abstract class GenericDAOImpl<E, PK extends Serializable> implements GenericDAO<E, PK> {

	/**
	 * 
	 */
	@PersistenceContext
	private EntityManager manager;
	
	/**
	 * @return
	 */
	protected abstract Class<E> getEntityClass();

	/* (non-Javadoc)
	 * @see com.ws.querydsl.dao.GenericDAO#save(java.lang.Object)
	 */
	@Override
	public E save(E pNewInstance) {
		this.manager.persist(pNewInstance);
		
		return pNewInstance;
	}

	/* (non-Javadoc)
	 * @see com.ws.querydsl.dao.GenericDAO#update(java.lang.Object)
	 */
	@Override
	public E update(E pTransientObject) {
		this.manager.persist(pTransientObject);
		return pTransientObject;
	}

	/* (non-Javadoc)
	 * @see com.ws.querydsl.dao.GenericDAO#delete(java.lang.Object)
	 */
	@Override
	public void delete(E pPersistentObject) {
		this.manager.remove(pPersistentObject);
		this.manager.flush();
	}

	/* (non-Javadoc)
	 * @see com.ws.querydsl.dao.GenericDAO#findById(java.io.Serializable)
	 */
	@Override
	public E findById(PK pId) {
		return this.manager.find(getEntityClass(), pId);
	}

}
